package com.tinxing.bp.commons.utils.util.page;

/**
 * @author YJH
 * @date 2019/12/6 22:37
 */

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tinxing.bp.commons.utils.ToolUtil;
import com.tinxing.bp.commons.utils.kit.HttpKit;

import javax.servlet.http.HttpServletRequest;
import java.util.Collections;


/**
 * 获取分页条件封装分页对象
 *
 * @author ZJL
 * @date 2019年10月25日 下午6:17:45
 */
public class PageFactory<T> {

    /**
     * 每页个数
     */
    private final static String PAGE_SIZE = "pageSize";

    /**
     * 当前页数
     */
    private final static String CURRENT_PAGE = "currentPage";

    /**
     * 排序字段
     */
    private final static String SORT = "sort";

    /**
     * 排序类型 true-升序 false-降序
     */
    private final static String ORDER = "order";

    /**
     * 获取默认的分页排序信息
     *
     * @param <T> 泛型实体类
     * @return 分页信息
     */
    public static <T> Page<T> getDefaultPage() {
        HttpServletRequest request = HttpKit.getRequest();
        int pageSize = 10;
        int current = 1;
        assert request != null;
        if (request.getParameter(PAGE_SIZE) != null) {
            pageSize = Integer.parseInt(request.getParameter(PAGE_SIZE));
        }
        if (request.getParameter(CURRENT_PAGE) != null) {
            current = Integer.parseInt(request.getParameter(CURRENT_PAGE));
        }
        String sort = request.getParameter(SORT);
        //asc或desc(升序或降序)
        boolean order = Boolean.parseBoolean(request.getParameter(ORDER));
        if (current <= 0) {
            current = 1;
        }
        Page<T> page = new Page<T>(current, pageSize);
        if (!ToolUtil.isEmpty(sort)) {
            OrderItem orderItem = new OrderItem();
            if (!order) {
                page.setOrders(Collections.singletonList(OrderItem.desc(sort)));
            } else {
                page.setOrders(Collections.singletonList(OrderItem.asc(sort)));
            }
        }
        return page;
    }

    public Page<T> defaultPage() {
        HttpServletRequest request = HttpKit.getRequest();
        int pageSize = 10;
        int current = 1;
        if (request.getParameter("pageSize") != null) {
            //每页多少条数据
            pageSize = Integer.valueOf(request.getParameter("pageSize"));
        }
        if (request.getParameter("currentPage") != null) {
            //当前页码
            current = Integer.valueOf(request.getParameter("currentPage"));
        }
        //排序字段名称
        String sort = request.getParameter("sort");
        //asc或desc(升序或降序)
        boolean order = Boolean.parseBoolean(request.getParameter("order"));
        if (current < 0) {
            current = 1;
        }
        if (ToolUtil.isEmpty(sort)) {
            Page<T> p = new Page<>(current, pageSize);
            return p;
        } else {
            Page<T> p = new Page<>(current, pageSize);
            if (!order) {
                p.setDesc(sort);

            } else {
                p.setAsc(sort);
            }
            return p;
        }
    }

}
